package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.shop.EbGroupBuyShop;
import com.zbkj.common.model.shop.EbGroupBuyShopBusinessHours;
import com.zbkj.common.model.shop.EbGroupBuyShopVerificationUser;
import com.zbkj.common.model.user.User;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.EbGroupBuyShopBusinessHoursRequest;
import com.zbkj.common.request.EbGroupBuyShopRequest;
import com.zbkj.common.response.*;
import com.zbkj.service.dao.shop.EbGroupBuyShopBusinessHoursDao;
import com.zbkj.service.dao.shop.EbGroupBuyShopDao;
import com.zbkj.service.dao.shop.EbGroupBuyShopVerificationUserDao;
import com.zbkj.service.service.*;
import com.zbkj.service.util.WeekdayTextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j

public class EbGroupBuyShopServiceImpl extends ServiceImpl<EbGroupBuyShopDao, EbGroupBuyShop>
        implements EbGroupBuyShopService {
    @Resource
    EbGroupBuyShopBusinessHoursDao hourDao;
    @Resource
    EbGroupBuyShopDao ebGroupBuyShopDao;
    @Resource
    EbGroupPurchaseService ebGroupPurchaseService;
    @Resource
    EbIndustryCategoryService ebIndustryCategoryService;
    @Autowired
    UserService userService;
    @Autowired
    MerchantService merchantService;
    @Autowired
    EbGroupBuyShopVerificationUserDao verificationUserDao;

    /**
     * 增加团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @Override
    public EbGroupBuyShop add(EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        log.info("add:{}", ebGroupBuyShopRequest.toString());
        Date date = new Date();
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        ebGroupBuyShopRequest.setMerId(merchant.getId());
        ebGroupBuyShopRequest.setMerName(merchant.getName());
        ebGroupBuyShopRequest.setStatus(0);
//        ebGroupBuyShopRequest.setBusinessStatus(3);
        ebGroupBuyShopRequest.setCreateTime(date);
        ebGroupBuyShopRequest.setUpdateTime(date);
        EbGroupBuyShop ebGroupBuyShop = new EbGroupBuyShop();
        BeanUtils.copyProperties(ebGroupBuyShopRequest, ebGroupBuyShop);
        if (ebGroupBuyShopRequest.getIndustryIdList() != null) {
            String industryIds = ebGroupBuyShopRequest.getIndustryIdList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setIndustryIds(industryIds);
        }
        if (ebGroupBuyShopRequest.getMainCategoryIdList() != null) {
            String mainCategoryIds = ebGroupBuyShopRequest.getMainCategoryIdList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setMainCategoryIds(mainCategoryIds);
        }
        if (ebGroupBuyShopRequest.getThreeLevelIndustryIdList() != null) {
            String threeLevelIndustryIds = ebGroupBuyShopRequest.getThreeLevelIndustryIdList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setThreeLevelIndustryIds(threeLevelIndustryIds);
        }
        if (ebGroupBuyShopRequest.getShopBannerList() != null) {
            String shopBanners = ebGroupBuyShopRequest.getShopBannerList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setShopBanners(shopBanners);
        }
        if (ebGroupBuyShopRequest.getShopImageList() != null) {
            String shopImages = ebGroupBuyShopRequest.getShopImageList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setShopImages(shopImages);
        }
        ebGroupBuyShopDao.insert(ebGroupBuyShop);
        if (CollectionUtils.isNotEmpty(ebGroupBuyShopRequest.getHoursList())) {
            for (EbGroupBuyShopBusinessHoursRequest hours : ebGroupBuyShopRequest.getHoursList()) {
                hours.setShopId(ebGroupBuyShop.getId());
                EbGroupBuyShopBusinessHours ebGroupBuyShopBusinessHours = new EbGroupBuyShopBusinessHours();
                BeanUtils.copyProperties(hours, ebGroupBuyShopBusinessHours);
                hourDao.insert(ebGroupBuyShopBusinessHours);
            }
        }
        return ebGroupBuyShop;
    }

    /**
     * 删除团购店铺
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        log.info("delete:{}", id);
        return removeById(id);
    }

    /**
     * 修改团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @Override
    public EbGroupBuyShop update(EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        log.info("update:{}", ebGroupBuyShopRequest.toString());
        EbGroupBuyShop shop = ebGroupBuyShopDao.selectById(ebGroupBuyShopRequest.getId());
        log.info("update shop:{}", shop.toString());
        Date date = new Date();
        ebGroupBuyShopRequest.setUpdateTime(date);
        EbGroupBuyShop ebGroupBuyShop = new EbGroupBuyShop();
        BeanUtils.copyProperties(ebGroupBuyShopRequest, ebGroupBuyShop);
        if (ebGroupBuyShopRequest.getIndustryIdList() != null) {
            String industryIds = ebGroupBuyShopRequest.getIndustryIdList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setIndustryIds(industryIds);
        }
        if (ebGroupBuyShopRequest.getMainCategoryIdList() != null) {
            String mainCategoryIds = ebGroupBuyShopRequest.getMainCategoryIdList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setMainCategoryIds(mainCategoryIds);
        }
        if (ebGroupBuyShopRequest.getThreeLevelIndustryIdList() != null) {
            String threeLevelIndustryIds = ebGroupBuyShopRequest.getThreeLevelIndustryIdList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setThreeLevelIndustryIds(threeLevelIndustryIds);
        }
        if (ebGroupBuyShopRequest.getShopBannerList() != null) {
            String shopBanners = ebGroupBuyShopRequest.getShopBannerList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setShopBanners(shopBanners);
        }
        if (ebGroupBuyShopRequest.getShopImageList() != null) {
            String shopImages = ebGroupBuyShopRequest.getShopImageList().stream()
                    .map(Object::toString)
                    .collect(Collectors.joining(","));
            ebGroupBuyShop.setShopImages(shopImages);
        }
        if (shop.areEbGroupBuyShopsEqual(ebGroupBuyShop)) {
            log.info("数据相同");
        } else {
            log.info("数据不一致");
            ebGroupBuyShop.setStatus(0);
        }
        ebGroupBuyShopDao.updateById(ebGroupBuyShop);
        if (CollectionUtils.isNotEmpty(ebGroupBuyShopRequest.getHoursList())) {
            hourDao.delete(new LambdaUpdateWrapper<EbGroupBuyShopBusinessHours>().eq(EbGroupBuyShopBusinessHours::getShopId, ebGroupBuyShop.getId()));
            for (EbGroupBuyShopBusinessHoursRequest hours : ebGroupBuyShopRequest.getHoursList()) {
                hours.setShopId(ebGroupBuyShop.getId());
                EbGroupBuyShopBusinessHours ebGroupBuyShopBusinessHours = new EbGroupBuyShopBusinessHours();
                BeanUtils.copyProperties(hours, ebGroupBuyShopBusinessHours);
                hourDao.insert(ebGroupBuyShopBusinessHours);
            }
        }
        return ebGroupBuyShopDao.selectById(ebGroupBuyShopRequest.getId());
    }

    /**
     * 修改营业信息
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @Override
    public EbGroupBuyShop updateBusiness(EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        log.info("updateBusiness:{}", ebGroupBuyShopRequest.toString());
        ebGroupBuyShopDao.update(null, new LambdaUpdateWrapper<EbGroupBuyShop>()
                .set(EbGroupBuyShop::getBusinessStatus, ebGroupBuyShopRequest.getBusinessStatus())
                .eq(EbGroupBuyShop::getId, ebGroupBuyShopRequest.getId())
        );
        //修改营业时间
        if (CollectionUtils.isNotEmpty(ebGroupBuyShopRequest.getHoursList())) {
            hourDao.delete(new LambdaUpdateWrapper<EbGroupBuyShopBusinessHours>().eq(EbGroupBuyShopBusinessHours::getShopId, ebGroupBuyShopRequest.getId()));
            for (EbGroupBuyShopBusinessHoursRequest hours : ebGroupBuyShopRequest.getHoursList()) {
                hours.setShopId(ebGroupBuyShopRequest.getId());
                EbGroupBuyShopBusinessHours ebGroupBuyShopBusinessHours = new EbGroupBuyShopBusinessHours();
                BeanUtils.copyProperties(hours, ebGroupBuyShopBusinessHours);
                hourDao.insert(ebGroupBuyShopBusinessHours);
            }
        }
        return ebGroupBuyShopDao.selectById(ebGroupBuyShopRequest.getId());
    }

    /**
     * 查看店铺详情
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @Override
    public EbGroupBuyShopResponse getDetails(EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        long id = ebGroupBuyShopRequest.getId();
        EbGroupBuyShopResponse ebGroupBuyShopResponse = getById(id);
        List<EbGroupPurchaseResponse> goodsList = ebGroupPurchaseService.getShopList(id);
        List<EbGroupPurchaseResponse> shopGoodsList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(goodsList)){
            goodsList.forEach(ebGroupPurchaseResponse -> {
                if (ebGroupPurchaseResponse.getStatus()) {
                    shopGoodsList.add(ebGroupPurchaseResponse);
                }
            });
        }
        ebGroupBuyShopResponse.setGoodsList(shopGoodsList);
        int salesVolume = 0;
        for (EbGroupPurchaseResponse ebGroupPurchaseResponse : ebGroupBuyShopResponse.getGoodsList()) {
            salesVolume = salesVolume + ebGroupPurchaseResponse.getSalesVolume();
        }
        ebGroupBuyShopResponse.setSalesVolume(salesVolume);
        List<SearchGroupBuyShopResponse> shopList = ebGroupBuyShopDao.getShopPage(ebGroupBuyShopRequest);
        if (CollectionUtils.isNotEmpty(shopList)) {
            //获取用户距离
            ebGroupBuyShopResponse.setUserDistance(shopList.get(0).getUserDistance());
        } else {
            ebGroupBuyShopResponse.setUserDistance("0");
        }
        return ebGroupBuyShopResponse;
    }

    /**
     * 删除核销人员
     *
     * @param ebGroupBuyShopVerificationUser
     * @return
     */
    @Override
    public Boolean delUserShopVerification(EbGroupBuyShopVerificationUser ebGroupBuyShopVerificationUser) {
        verificationUserDao.delete(new LambdaUpdateWrapper<EbGroupBuyShopVerificationUser>()
                .eq(EbGroupBuyShopVerificationUser::getMerId, ebGroupBuyShopVerificationUser.getMerId())
                .eq(EbGroupBuyShopVerificationUser::getId, ebGroupBuyShopVerificationUser.getId())
        );
        return true;
    }

    /**
     * 查询门店关联的用户信息
     *
     * @param merId
     * @return
     */
    @Override
    public List<EbGroupBuyShopVerificationUserResponse> getShopVerificationUserList(Integer merId) {
        List<EbGroupBuyShopVerificationUser> list = verificationUserDao.selectList(new LambdaQueryWrapper<EbGroupBuyShopVerificationUser>()
                .eq(EbGroupBuyShopVerificationUser::getMerId, merId)
        );
        if (CollectionUtil.isNotEmpty(list)) {
            List<EbGroupBuyShopVerificationUserResponse> responseList = new ArrayList<>();
            Map<Long, List<EbGroupBuyShopVerificationUser>> groupedByShopId = list.stream()
                    .collect(Collectors.groupingBy(EbGroupBuyShopVerificationUser::getShopId));
            for (Long shopId : groupedByShopId.keySet()) {
                //查询对应门店信息
                EbGroupBuyShopVerificationUserResponse response = new EbGroupBuyShopVerificationUserResponse();
                EbGroupBuyShop shop = ebGroupBuyShopDao.selectById(shopId);
                response.setId(shopId);
                response.setName(shop.getShopName());
                response.setList(groupedByShopId.get(shopId));
                responseList.add(response);
            }
            return responseList;
        }
        return Collections.emptyList();
    }

    /**
     * 查询用户关联的门店信息
     *
     * @param uid
     * @return
     */
    @Override
    public List<EbGroupBuyShopVerificationUserResponse> getUserShopVerification(Integer uid) {
        List<EbGroupBuyShopVerificationUser> list = verificationUserDao.selectList(new LambdaQueryWrapper<EbGroupBuyShopVerificationUser>()
                .eq(EbGroupBuyShopVerificationUser::getUid, uid)
        );
        if (CollectionUtil.isNotEmpty(list)) {
            List<EbGroupBuyShopVerificationUserResponse> responseList = new ArrayList<>();
            Map<Long, List<EbGroupBuyShopVerificationUser>> groupedByShopId = list.stream()
                    .collect(Collectors.groupingBy(EbGroupBuyShopVerificationUser::getShopId));
            for (Long shopId : groupedByShopId.keySet()) {
                //查询对应门店信息
                EbGroupBuyShopVerificationUserResponse response = new EbGroupBuyShopVerificationUserResponse();
                EbGroupBuyShop shop = ebGroupBuyShopDao.selectById(shopId);
                response.setId(shopId);
                response.setName(shop.getShopName());
                response.setList(groupedByShopId.get(shopId));
                responseList.add(response);
            }
            return responseList;
        }
        return Collections.emptyList();
    }

    @Override
    public List<Integer> getUserShopVerificationIdlIst(Integer id) {
        List<Integer> list = verificationUserDao.selectObjs(new LambdaQueryWrapper<EbGroupBuyShopVerificationUser>()
                        .select(EbGroupBuyShopVerificationUser::getMerId)
                        .eq(EbGroupBuyShopVerificationUser::getUid, id))
                .stream()
                .map(obj -> (Integer) obj)
                .collect(Collectors.toList());

        return list;
    }

    /**
     * 添加核销员信息
     *
     * @param ebGroupBuyShopVerificationUser
     * @return
     */
    @Override
    public EbGroupBuyShopVerificationUser addUserShopVerification(EbGroupBuyShopVerificationUser ebGroupBuyShopVerificationUser) {
        User user = userService.getByPhone(ebGroupBuyShopVerificationUser.getPhone());
        if (user == null) {
            throw new CrmebException("添加的核销员未注册");
        }
        ebGroupBuyShopVerificationUser.setUid(user.getId());
        ebGroupBuyShopVerificationUser.setUserUrl(user.getAvatar());
        ebGroupBuyShopVerificationUser.setUserName(user.getNickname());
        Date now = new Date();
        ebGroupBuyShopVerificationUser.setCreateTime(now);
        ebGroupBuyShopVerificationUser.setUpdateTime(now);
        verificationUserDao.insert(ebGroupBuyShopVerificationUser);
        return ebGroupBuyShopVerificationUser;
    }

    @Override
    public EbGroupBuyShopVerificationUser setUserShopVerification(EbGroupBuyShopVerificationUser ebGroupBuyShopVerificationUser) {
        EbGroupBuyShopVerificationUser shopVerificationUser = verificationUserDao.selectById(ebGroupBuyShopVerificationUser.getId());

        User user = userService.getByPhone(ebGroupBuyShopVerificationUser.getPhone());
        if (user == null) {
            throw new CrmebException("添加的核销员未注册");
        }
        shopVerificationUser.setUid(user.getId());
        shopVerificationUser.setUserUrl(user.getAvatar());
        shopVerificationUser.setUserName(user.getNickname());
        shopVerificationUser.setPhone(user.getPhone());
        Date now = new Date();
        shopVerificationUser.setUpdateTime(now);
        verificationUserDao.updateById(shopVerificationUser);
        return ebGroupBuyShopVerificationUser;
    }

    @Override
    public Map<String, Object> getStatusCount() {
        return Collections.emptyMap();
    }

    /**
     * 修改团购店铺
     *
     * @param ebGroupBuyShopRequest
     * @return
     */
    @Override
    public EbGroupBuyShop check(EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        log.info("check:{}", ebGroupBuyShopRequest.toString());
        if (ebGroupBuyShopRequest.getId() == null) {
            throw new IllegalArgumentException("店铺ID不能为空");
        }
        ebGroupBuyShopDao.update(null, new LambdaUpdateWrapper<EbGroupBuyShop>()
                .set(EbGroupBuyShop::getStatus, ebGroupBuyShopRequest.getStatus())
                .set(ebGroupBuyShopRequest.getRemarks() != null,
                        EbGroupBuyShop::getRemarks, ebGroupBuyShopRequest.getRemarks())
                .set(EbGroupBuyShop::getRegisterTime, new Date())
                .set(EbGroupBuyShop::getUpdateTime, new Date())
                .eq(EbGroupBuyShop::getId, ebGroupBuyShopRequest.getId())
        );

        return ebGroupBuyShopDao.selectById(ebGroupBuyShopRequest.getId());
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public EbGroupBuyShopResponse getById(Long id) {
        log.info("getById:{}", id);
        EbGroupBuyShopResponse ebGroupBuyShopResponse = new EbGroupBuyShopResponse();
        EbGroupBuyShop ebGroupBuyShop = ebGroupBuyShopDao.selectById(id);
        BeanUtils.copyProperties(ebGroupBuyShop, ebGroupBuyShopResponse);
        getInfo(ebGroupBuyShop, ebGroupBuyShopResponse);
        WeekdayTextUtil weekdayTextUtil = new WeekdayTextUtil();
        ebGroupBuyShopResponse.setShopHoursText(weekdayTextUtil.getShopHoursText(ebGroupBuyShopResponse.getHoursList()));
        // 重置装修状态
        ebGroupBuyShopResponse.setBusinessStatus(getBusinessStatus(ebGroupBuyShop.getId()));

        return ebGroupBuyShopResponse;
    }

    /**
     * 查询详情
     *
     * @param id
     * @return
     */
    @Override
    public Integer getBusinessStatus(Long id) {
        EbGroupBuyShop ebGroupBuyShop = ebGroupBuyShopDao.selectById(id);
        List<EbGroupBuyShopBusinessHours> hoursList = hourDao.selectList(new LambdaQueryWrapper<EbGroupBuyShopBusinessHours>()
                .eq(EbGroupBuyShopBusinessHours::getShopId, id));
        if (ebGroupBuyShop.getBusinessStatus() == 1 ||
                ebGroupBuyShop.getBusinessStatus() == 4) {
            ebGroupBuyShop.setBusinessStatus(getHoursStatus(hoursList));
        }
        return ebGroupBuyShop.getBusinessStatus();
    }

    /**
     * 查询列表
     *
     * @return
     */
    @Override
    public List<EbGroupBuyShopResponse> getList(EbGroupBuyShopRequest ebGroupBuyShopRequest) {
        User user = userService.getInfo();
        Merchant merchant = merchantService.getPhoneInfo(user.getPhone());
        if (merchant == null) {
            return new ArrayList<>();
        }
        List<EbGroupBuyShop> list = ebGroupBuyShopDao.selectList(new LambdaQueryWrapper<EbGroupBuyShop>()
                .eq(EbGroupBuyShop::getMerId, merchant.getId())
                .orderByDesc(EbGroupBuyShop::getId)
        );
        List<EbGroupBuyShopResponse> responseList = new ArrayList<>();
        for (EbGroupBuyShop ebGroupBuyShop : list) {
            EbGroupBuyShopResponse ebGroupBuyShopResponse = new EbGroupBuyShopResponse();
            BeanUtils.copyProperties(ebGroupBuyShop, ebGroupBuyShopResponse);
            getInfo(ebGroupBuyShop, ebGroupBuyShopResponse);
            responseList.add(ebGroupBuyShopResponse);
        }
        return responseList;
    }

    /**
     * 构建基本信息
     *
     * @param ebGroupBuyShop
     * @param ebGroupBuyShopResponse
     */
    private void getInfo(EbGroupBuyShop ebGroupBuyShop, EbGroupBuyShopResponse ebGroupBuyShopResponse) {
        List<Integer> idList = getIntList(ebGroupBuyShop.getIndustryIds());
        ebGroupBuyShopResponse.setIndustryNameList(ebIndustryCategoryService.getIndustryList(idList));
        ebGroupBuyShopResponse.setIndustryIdList(idList);
        List<Integer> messageIdList = getIntList(ebGroupBuyShop.getMainCategoryIds());
        List<Integer> threeLevelIndustryIdList = getIntList(ebGroupBuyShop.getThreeLevelIndustryIds());
        ebGroupBuyShopResponse.setMainCategoryNameList(ebIndustryCategoryService.getIndustryList(messageIdList));
        ebGroupBuyShopResponse.setThreeLevelIndustryNameList(ebIndustryCategoryService.getIndustryList(threeLevelIndustryIdList));
        ebGroupBuyShopResponse.setMainCategoryIdList(messageIdList);
        ebGroupBuyShopResponse.setThreeLevelIndustryIdList(threeLevelIndustryIdList);
        ebGroupBuyShopResponse.setShopImageList(getStrList(ebGroupBuyShop.getShopImages()));
        ebGroupBuyShopResponse.setShopBannerList(getStrList(ebGroupBuyShop.getShopBanners()));
        List<EbGroupBuyShopBusinessHoursResponse> hoursResponseList = new ArrayList<>();
        //渲染营业时间
        List<EbGroupBuyShopBusinessHours> hoursList = hourDao.selectList(new LambdaQueryWrapper<EbGroupBuyShopBusinessHours>()
                .eq(EbGroupBuyShopBusinessHours::getShopId, ebGroupBuyShop.getId()));
        for (EbGroupBuyShopBusinessHours hours : hoursList) {
            EbGroupBuyShopBusinessHoursResponse ebGroupBuyShopBusinessHoursResponse = new EbGroupBuyShopBusinessHoursResponse();
            BeanUtils.copyProperties(hours, ebGroupBuyShopBusinessHoursResponse);
            hoursResponseList.add(ebGroupBuyShopBusinessHoursResponse);
        }
        ebGroupBuyShopResponse.setHoursList(hoursResponseList);
    }

    /**
     * 获取营业时间
     *
     * @param hoursList
     * @return
     */
    public Integer getHoursStatus(List<EbGroupBuyShopBusinessHours> hoursList) {
        //是否在营业时间内 0否 1是
        int status = 4;
        LocalTime currentTime = LocalTime.now(); // 使用正确的方式获取当前时间
        for (EbGroupBuyShopBusinessHours hours : hoursList) {
            // 营业星期：1,2,3,4,5,6,7 对应星期几
            Integer dayOfWeek = hours.getDayOfWeek(); // 假设返回 1-7
            // 开始时间(00:00)
            LocalTime openTime = hours.getOpenTime(); // 确保字段类型为 LocalTime
            // 结束时间(20:00)
            LocalTime closeTime = hours.getCloseTime(); // 确保字段类型为 LocalTime
            // 判断当前星期几是否匹配（1=周一 ... 7=周日）
            int currentDayOfWeek = java.time.LocalDate.now().getDayOfWeek().getValue();
            if (dayOfWeek != null && dayOfWeek == currentDayOfWeek) {
                // 判断当前时间是否在营业时间段内
                if (currentTime.isAfter(openTime) && currentTime.isBefore(closeTime)) {
                    status = 1; // 在营业时间内
                    return status;
                }
            }
        }

        return status;
    }

    private List<String> getStrList(String strs) {
        List<String> idList = new ArrayList<>();
        if (strs != null && !strs.isEmpty()) {
            idList = Arrays.stream(strs.split(","))
                    .map(String::valueOf)
                    .collect(Collectors.toList());
        }
        return idList;
    }

    private List<Integer> getIntList(String strs) {
        List<Integer> idList = new ArrayList<>();
        if (strs != null && !strs.isEmpty()) {
            idList = Arrays.stream(strs.trim().split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
        }
        return idList;
    }

    /**
     * 分页查询
     *
     * @return
     */
    @Override
    public PageInfo<EbGroupBuyShopResponse> getPage(EbGroupBuyShopRequest request) {
        log.info("getPage: page={}, size={}", request.getPage(), request.getLimit());
        Page<EbGroupBuyShopResponse> page = PageHelper.startPage(request.getPage(), request.getLimit());
        List<EbGroupBuyShop> list = ebGroupBuyShopDao.selectList(new LambdaQueryWrapper<EbGroupBuyShop>()
                .eq(request.getMerId() != null, EbGroupBuyShop::getMerId, request.getMerId())
                .eq(request.getStatus() != null, EbGroupBuyShop::getStatus, request.getStatus())
                .eq(request.getBusinessStatus() != null, EbGroupBuyShop::getBusinessStatus, request.getBusinessStatus())
                .like(StringUtils.isNotBlank(request.getShopName()), EbGroupBuyShop::getShopName, request.getShopName())
                .like(StringUtils.isNotBlank(request.getContactName()), EbGroupBuyShop::getContactName, request.getContactName())
                .like(StringUtils.isNotBlank(request.getMerName()), EbGroupBuyShop::getMerName, request.getMerName())
                .like(StringUtils.isNotBlank(request.getContactPhone()), EbGroupBuyShop::getContactPhone, request.getContactPhone())
                .between(request.getStartRegisterTime() != null, EbGroupBuyShop::getRegisterTime, request.getStartRegisterTime(), request.getEndRegisterTime())
                .apply(request.getIndustryId() != null, "FIND_IN_SET({0}, industry_ids)", request.getIndustryId())
                .orderByDesc(EbGroupBuyShop::getUpdateTime)

        );
        List<EbGroupBuyShopResponse> responseList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(list)) {
            for (EbGroupBuyShop ebGroupBuyShop : list) {
                EbGroupBuyShopResponse ebGroupBuyShopResponse = new EbGroupBuyShopResponse();
                BeanUtils.copyProperties(ebGroupBuyShop, ebGroupBuyShopResponse);
                List<Integer> idList = getIntList(ebGroupBuyShop.getIndustryIds());
                ebGroupBuyShopResponse.setIndustryNameList(ebIndustryCategoryService.getIndustryList(idList));
                ebGroupBuyShopResponse.setIndustryIdList(idList);
                List<Integer> messageIdList = getIntList(ebGroupBuyShop.getMainCategoryIds());
                ebGroupBuyShopResponse.setMainCategoryNameList(ebIndustryCategoryService.getIndustryList(messageIdList));
                ebGroupBuyShopResponse.setMainCategoryIdList(messageIdList);
                List<EbGroupBuyShopBusinessHours> hoursList = hourDao.selectList(new LambdaQueryWrapper<EbGroupBuyShopBusinessHours>()
                        .eq(EbGroupBuyShopBusinessHours::getShopId, ebGroupBuyShop.getId()));
                if (ebGroupBuyShopResponse.getBusinessStatus() == 1 ||
                        ebGroupBuyShopResponse.getBusinessStatus() == 4) {
                    ebGroupBuyShopResponse.setBusinessStatus(getHoursStatus(hoursList));
                }
                responseList.add(ebGroupBuyShopResponse);
            }
        }
        return CommonPage.copyPageInfo(page, responseList);
    }

    /**
     * 首页搜索店铺列表
     *
     * @param request
     * @return
     */
    @Override
    public PageInfo<SearchGroupBuyShopResponse> getShopPage(EbGroupBuyShopRequest request) {
        log.info("getPage: page={}, size={}", request.getPage(), request.getLimit());
        Page<EbGroupBuyShopResponse> page = PageHelper.startPage(request.getPage(), request.getLimit());
        if (request.getIndustryId() != null && request.getIndustryId() == -3) {
            request.setIndustryId(null);
        }
        if (request.getMainCategoryId() != null && request.getMainCategoryId() == -3) {
            request.setMainCategoryId(null);
        }
        if (request.getThreeLevelIndustryId() != null && request.getThreeLevelIndustryId() == -3) {
            request.setThreeLevelIndustryId(null);
        }
        List<SearchGroupBuyShopResponse> list = ebGroupBuyShopDao.getShopPage(request);
        List<SearchGroupBuyShopResponse> responseList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(list)) {
            for (SearchGroupBuyShopResponse ebGroupBuyShop : list) {
                List<Integer> idList = getIntList(ebGroupBuyShop.getIndustryIds());
                List<Integer> mIdList = getIntList(ebGroupBuyShop.getMainCategoryIds());
                ebGroupBuyShop.setIndustryNameList(ebIndustryCategoryService.getIndustryList(idList));
                ebGroupBuyShop.setMainCategoryNameList(ebIndustryCategoryService.getIndustryList(mIdList));
                List<EbGroupBuyShopBusinessHours> hoursList = hourDao.selectList(new LambdaQueryWrapper<EbGroupBuyShopBusinessHours>()
                        .eq(EbGroupBuyShopBusinessHours::getShopId, ebGroupBuyShop.getId()));
                if (ebGroupBuyShop.getBusinessStatus() == 1 ||
                        ebGroupBuyShop.getBusinessStatus() == 4) {
                    ebGroupBuyShop.setBusinessStatus(getHoursStatus(hoursList));
                }
//                IFNULL(MAX(goods.group_price), 0) AS maxPrice,
//                IFNULL(MIN(goods.group_price), 0) AS minPrice,
//                IFNULL(SUM(goods.sales_volume), 0) AS totalSales
//                BigDecimal maxPrice = BigDecimal.ZERO;
//                BigDecimal minPrice = BigDecimal.ZERO;
//                BigDecimal totalSales = BigDecimal.ZERO;
                List<EbGroupPurchaseResponse> goodsList = ebGroupPurchaseService.getShopList(ebGroupBuyShop.getId());
                if (CollectionUtil.isNotEmpty(goodsList)) {
                    List<EbGroupPurchaseResponse> shopGoodsList = new ArrayList<>();
                    //店铺销量
                    int salesVolume = 0;
                    for (EbGroupPurchaseResponse ebGroupPurchaseResponse : goodsList) {
                        if (!ebGroupPurchaseResponse.getStatus()) {
                            break;
                        }
                        salesVolume = salesVolume + ebGroupPurchaseResponse.getSalesVolume();
                        if (shopGoodsList.size() < 3) {
                            //仅展示3个商品
                            shopGoodsList.add(ebGroupPurchaseResponse);
                        }
                    }
                    ebGroupBuyShop.setSalesVolume(salesVolume);
                    ebGroupBuyShop.setGoodsList(shopGoodsList);
                    responseList.add(ebGroupBuyShop);
                }

            }
        }
        return CommonPage.copyPageInfo(page, responseList);
    }

    @Override
    public Integer getCount(Integer id) {
        return ebGroupBuyShopDao.selectCount(new LambdaQueryWrapper<EbGroupBuyShop>()
                .eq(EbGroupBuyShop::getMerId, id)
        );
    }
}